// pages/certification/certification.js
const config = require('../../config/api.js'),
  qiniuUploader = require("../../utils/qiniuUploader");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgStu: "../../images/upload.png",
    imgIden: "../../images/upload.png",
    stuStr: "",
    idenStr: "",
    imgType: "",
    uploadToken: null,
    status: 0,
    sexItem: [{
        name: '女',
        value: '0',
        checked:'true'
      },
      {
        name: '男',
        value: '1'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.qiniuToken();
    this.setData({
      sex:this.data.sexItem[0].value
    })
    console.log(this.data.sex)
  },

  submit: function() {
    let bool = ((this.data.imgStu != "../../images/upload.png") && (this.data.imgIden != "../../images/upload.png")),
      that = this;
      console.log(that.data.sex)
    if (bool) {
      wx.request({
        url: config.uploadImg,
        method: "POST",
        data: {
          stu_card: that.data.stuStr,
          id_card: that.data.idenStr,
          sex:that.data.sex
        },
        header: {
          'Authorization': 'Bearer ' + wx.getStorageSync('token')
        },
        success(res) {
          console.log(res)
          wx.showToast({
            title: '上传成功',
            icon: 'none',
            duration: 1500,
            success: function() {
              wx.reLaunch({
                url: '/pages/personal/personal',
              })
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '请上传身份证和学生证照片',
        icon: 'none',
        duration: 1000
      })
    }
  },
  radioChange:function(e){
    this.setData({
      sex:e.detail.value
    })
  },
  //请求七牛云token
  qiniuToken() {
    wx.request({
      url: "https://api.mokyun.com/qiniu/upload/token",
      method: "GET",
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: res => {
        const uploadToken = res.data.token
        this.setData({
          uploadToken: uploadToken
        })
      }
    })
  },

  //上传图片到七牛云
  updateImg: function(filePath, uptoken) {
    var that = this
    qiniuUploader.upload(filePath, (res) => {
      if (that.data.imgType === "student") {
        that.setData({
          stuStr: "https://cdn1-open.mokyun.com" + res.imageURL,
          imgStu: "https://cdn1-open.mokyun.com" + res.imageURL
        })
      } else {
        that.setData({
          idenStr: "https://cdn1-open.mokyun.com" + res.imageURL,
          imgIden: "https://cdn1-open.mokyun.com" + res.imageURL
        })
      }
    }, (error) => {
      console.log('error: ' + error);
    }, {
      // key: pciName,
      region: 'ECN',
      uptoken: uptoken,
      uploadURL: 'https://up.qbox.me',
    });
  },

  //上传学生证
  uploadStu(e, type) {
    let that = this;
    wx.chooseImage({
      success: function(res) {
        let url = res.tempFilePaths[0];
        that.updateImg(url, that.data.uploadToken)
        that.setData({
          imgType: "student"
        })
      },
    })
  },

  //上传身份证
  uploadIden() {
    let that = this;
    wx.chooseImage({
      success: function(res) {
        let url = res.tempFilePaths[0];
        that.updateImg(url, that.data.uploadToken)
        that.setData({
          imgType: "identity"
        })
      },
    })
  },


})